<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>题目管理页面</title>
    <script src="jquery-3.6.0.js"></script>
    <script src="layui/layui.js"></script>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<input hidden  id="creator" name="creator">
<input hidden id="updater" name="updater">
<!--隐藏表单,点击添加按钮内容-->
<form class="layui-form" action="" style="display: none" id="addQuestion" lay-filter="addQuestionFormFilter">
    <div class="layui-form-item">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" name="question" required lay-verify="required" placeholder="请输入问题" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-inline">
            <input type="text" name="optionA" required lay-verify="required" placeholder="请输入选项A" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-inline">
            <input type="text" name="optionB" required lay-verify="required" placeholder="请输入选项B" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-inline">
            <input type="text" name="optionC" required lay-verify="required" placeholder="请输入选项C" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-inline">
            <input type="text" name="optionD" required lay-verify="required" placeholder="请输入选项D" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-inline">
            <select name="type">
                <option value="1">前15题</option>
                <option value="2">后15题</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <input type="checkbox" name="status" lay-skin="switch" value="0" lay-text="启用|禁用" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit lay-filter="addQuestionFilter">立即提交</button>
        </div>
    </div>
</form>
<form class="layui-form" action="" style="display: none" id="editQuestion" lay-filter="editQuestionFormFilter">
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">id</label>
        <div class="layui-input-inline">
            <input type="text" name="id" required lay-verify="required" placeholder="请输入id" autocomplete="off"
                   class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" name="question" required lay-verify="required" placeholder="请输入问题" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-inline">
            <input type="text" name="optionA" required lay-verify="required" placeholder="请输入选项A" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-inline">
            <input type="text" name="optionB" required lay-verify="required" placeholder="请输入选项B" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-inline">
            <input type="text" name="optionC" required lay-verify="required" placeholder="请输入选项C" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-inline">
            <input type="text" name="optionD" required lay-verify="required" placeholder="请输入选项D" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-inline">
            <select name="type">
                <option value="1">前15题</option>
                <option value="2">后15题</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <input type="checkbox" name="status" lay-skin="switch" value="0" lay-text="启用|禁用" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit lay-filter="editQuestionFilter">立即提交</button>
        </div>
    </div>
</form>
<form class="layui-form" action="" id="search" lay-filter="searchQuestionFormFilter">
    <div class="layui-form-item">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" name="question"   placeholder="请输入问题" autocomplete="off"
                   class="layui-input" >
        </div>
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" name="creator"   placeholder="请输入创建人" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" lay-submit lay-filter="search">查询</button>
            <button type="reset" id="searchReset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<table id="question" lay-filter="question"></table>
</body>
<!--行内按钮-->
<script type="text/html" id="operation">
    <a shiro:hasPermission="character:question:del" class="layui-btn layui-btn-dange layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    <a shiro:hasPermission="character:question:edit" class="layui-btn layui-btn-xs " lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
</script>
<!--工具栏按钮-->
<script type="text/html" id="toolbar">
    <a shiro:hasPermission="character:question:add"  class="layui-btn  layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-addition"></i>添加</a>
    <a shiro:hasPermission="character:question:del" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delBatch"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script>
    layui.use(['table', 'form', 'layer', 'laydate'], function () {
        var table = layui.table;
        let form = layui.form;
        let layer = layui.layer;
        let date = layui.laydate;
        //第一个实例
        table.render({
            elem: '#question'
            , height: 400
            , url: '/question' //数据接口
            , page: true//开启分页
            , limit: 5
            , limits: [5, 10, 15, 20]
            , toolbar: '#toolbar'
            , cols: [
                [ //表头
                    {field: 'select', width: 50, type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', width: 80, sort: true}
                    , {field: 'question', title: '问题', width: 120}
                    , {field: 'optionA', title: '选项A', width: 120}
                    , {field: 'optionB', title: '选项B', width: 120}
                    , {field: 'optionC', title: '选项C', width: 120}
                    , {field: 'optionD', title: '选项D', width: 120}
                    , {
                    field: 'type', title: '类型', width: 80,
                    templet: function (type) {
                        if (type.type == 1) {
                            return 'FPA前十五题';
                        } else {
                            return 'FPA后十五题';
                        }
                    }
                }
                    , {
                    field: 'status', title: '状态', width: 80,
                    templet: function (status) {
                        if (status.status == 0) {
                            return '<a class="layui-btn layui-btn-xs layui-btn-warm" >可用</a>';
                        } else {
                            return '<a class="layui-btn layui-btn-xs layui-btn-disabled">失效</a>';
                        }
                    }
                }
                    , {field: 'createTime', title: '创建时间', width: 80}
                    , {field: 'creator', title: '创建人', width: 80}
                    , {field: 'updateTime', title: '修改时间', width: 80}
                    , {field: 'updater', title: '修改人', width: 80}
                    , {field: 'operation', title: '操作', toolbar: '#operation',width:150}
                ]
            ]
        });
        date.render({
            elem: '#createTime',
            type: 'datetime'
        });
        //工具栏按钮添加和批量删除
        table.on('toolbar(question)', function (obj) {
            let check = table.checkStatus(obj.config.id)
            let data = check.data;
            let layEvent = obj.event;
            if (layEvent === 'add') {
                layer.open({
                    title: '添加问题',
                    type: 1,
                    area: ['550px', '400px'],
                    offset: '200px',
                    content: $("#addQuestion")
                });
                // 栏添加按钮弹出表单提交按钮触发事件
                form.on('submit(addQuestionFilter)', function (data) {
                    if(data.field.status!=0){
                        data.field.status=1;
                        console.log(data.field.status);
                    }
                    data.field.creator = $("#creator").val();
                    $.ajax({
                        url: "/question/insert",
                        contentType: 'application/json;charset=UTF-8',
                        data: JSON.stringify(data.field),
                        dataType: "json",
                        type: "post",
                        success: function (result) {
                            if (result.code == '0') {
                                layer.closeAll();
                                $("#addQuestion")[0].reset();
                                form.render();
                                table.reload('question');
                                layer.msg(result.msg, {icon: 6});
                            } else {
                                layer.msg(result.msg, {icon: 5});
                            }
                        }
                    })
                })
            } else if (layEvent === 'delBatch') {
                if (data.length === 0) {
                    layer.msg("请选择一行数据");
                } else {
                    let idList = [];
                    for (let i = 0; i < data.length; i++) {
                        idList[i] = data[i].id;
                    }
                    layer.confirm('真的删除吗', function (index) {
                        $.ajax({
                            url: "/question/delete",
                            type: "delete",
                            data: {idList: idList},
                            dataType: "json",
                            success: function (result) {
                                if (result.code == '0') {
                                    layer.closeAll();
                                    table.reload('question', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                    layer.msg(result.msg, {icon: 6});
                                } else {
                                    layer.msg(result.msg, {icon: 5});
                                }
                            }
                        })
                    })
                }
            }
        })
        //行内按钮修改删除
        table.on('tool(question)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'edit') {
                form.val('editQuestionFormFilter',{
                    id:data.id,
                    question:data.question,
                    optionA:data.optionA,
                    optionB:data.optionB,
                    optionC:data.optionC,
                    optionD:data.optionD,
                    type:data.type,
                    status:data.status==0?true:false,
                    updater:data.creator
                })
                layer.open({
                    title: '修改问题',
                    type: 1,
                    area: ['550px', '400px'],
                    offset: '200px',
                    content: $("#editQuestion")
                });

                // 栏添加按钮弹出表单提交按钮触发事件
                form.on('submit(editQuestionFilter)', function (data) {
                    if(data.field.status!=0){
                        data.field.status=1;
                        console.log(data.field.status);
                    }
                    data.field.updater = $("#updater").val();
                    $.ajax({
                        url: "/question/update",
                        contentType: 'application/json;charset=UTF-8',
                        data: JSON.stringify(data.field),
                        dataType: "json",
                        type: "put",
                        success: function (result) {
                            if (result.code == '0') {
                                layer.closeAll();
                                $("#editQuestion")[0].reset();
                                form.render();
                                table.reload('question');
                                layer.msg(result.msg, {icon: 6});
                            } else {
                                layer.msg(result.msg, {icon: 5});
                            }
                        }
                    })
                })
            } else if (layEvent === 'del') {
                if (data.length === 0) {
                    layer.msg("请选择一行数据");
                } else {
                    let idList = [];
                    idList.push(data.id);
                    layer.confirm('真的删除吗', function (index) {
                        $.ajax({
                            url: "/question/delete",
                            type: "delete",
                            data: {idList: idList},
                            dataType: "json",
                            success: function (result) {
                                if (result.code == '0') {
                                    layer.closeAll();
                                    table.reload('question', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                    layer.msg(result.msg, {icon: 6});
                                } else {
                                    layer.msg(result.msg, {icon: 5});
                                }
                            }
                        })
                    })
                }
            }
        })
        //查询
        form.on('submit(search)', function (data) {
            table.reload('question', {
                where: {
                    question: data.field.question,
                    creator: data.field.creator,
                    page: 1,
                    limit: 5
                }
            });
            return false;
        });
        //重置按钮
        $(document).on('click', '#searchReset', function () {
            table.reload('question', {
                where: {
                    question: null,
                    creator: null
                }
            })
        });
    });
</script>
</html>
